package so.shanku.essential.adapter;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.view.CircleImageView;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;

/**
 * Created by Jaelyn on 2015/9/9 0009.
 */
public class CommentsAdapter extends MyBaseAdapter {
    private int flag;

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public CommentsAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        CommentsAdapterViewHolder commentsAdapterViewHolder;
        if (view == null) {
            commentsAdapterViewHolder = new CommentsAdapterViewHolder();
            view = layoutInflater.inflate(R.layout.findcomments_item, null);
            ViewUtils.inject(commentsAdapterViewHolder, view);
            view.setTag(commentsAdapterViewHolder);
        } else {
            commentsAdapterViewHolder = (CommentsAdapterViewHolder) view.getTag();
        }
        bindData(i, commentsAdapterViewHolder);
        return view;
    }

    private void bindData(int i, CommentsAdapterViewHolder goodsCommentsAdapterViewHolder) {
        JsonMap map = (JsonMap) datas.get(i);
        String name = map.getString("Name");
        String headlog = map.getStringNoNull("Header");
        if (i == 0 && flag == 1) {
            goodsCommentsAdapterViewHolder.userName_tv.getPaint().setAntiAlias(true);
            goodsCommentsAdapterViewHolder.userName_tv.getPaint().setFakeBoldText(true);
            goodsCommentsAdapterViewHolder.commit_time_tv.getPaint().setAntiAlias(true);
            goodsCommentsAdapterViewHolder.commit_time_tv.getPaint().setFakeBoldText(true);
            goodsCommentsAdapterViewHolder.comments_content_tv.getPaint().setAntiAlias(true);
            goodsCommentsAdapterViewHolder.comments_content_tv.getPaint().setFakeBoldText(true);
            goodsCommentsAdapterViewHolder.comments_content_tv.setTextColor(context.getResources().getColor(R.color.darkGray1));

        } else {
            goodsCommentsAdapterViewHolder.userName_tv.getPaint().setFakeBoldText(false);
            goodsCommentsAdapterViewHolder.comments_content_tv.getPaint().setFakeBoldText(false);
            goodsCommentsAdapterViewHolder.commit_time_tv.getPaint().setFakeBoldText(false);
            goodsCommentsAdapterViewHolder.comments_content_tv.setTextColor(context.getResources().getColor(R.color.darkGray));
        }
        if (TextUtils.isEmpty(name)) {
            goodsCommentsAdapterViewHolder.userName_tv.setText(context.getString(R.string.no_name) + i);
        } else {
//			if(StringUtil.isMobileNO(name)){
//				name=StringUtil.secretMobilePhone(name);
//			}
            goodsCommentsAdapterViewHolder.userName_tv.setText(name);
        }
        goodsCommentsAdapterViewHolder.commit_time_tv.setText(map.getStringNoNull("CreateTimeStr"));

        if (MyApplication.getInstance().getBitmapHashMap().containsKey(headlog)) {
            goodsCommentsAdapterViewHolder.user_header_iv.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(headlog)));
        } else {
            ImageLoader.getInstance().displayImage(headlog, goodsCommentsAdapterViewHolder.user_header_iv, MyApplication.getInstance().getUserHeadOption(), new ImageLoaderBitmapCallBack());
        }

        goodsCommentsAdapterViewHolder.userName_tv.setSelected(map.getBoolean("isVendor"));
        String commentsContent = map.getStringNoNull("Content");
        String returnName = map.getStringNoNull("ReturnName");
        if (!TextUtils.isEmpty(returnName)) {
            String str_fomart = "<b>回复@%s：</b>%s";
            String msg = String.format(str_fomart, returnName, commentsContent);
            goodsCommentsAdapterViewHolder.comments_content_tv.setText(Html.fromHtml(msg));
        } else {
            goodsCommentsAdapterViewHolder.comments_content_tv.setText(commentsContent);
        }
    }

    public SpannableString setTextBold(String content, int startIndex, int endIndex) {
        if (TextUtils.isEmpty(content) || startIndex < 0 || endIndex >= content.length() || startIndex >= endIndex) {
            return null;
        }

        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spannableString;
    }

    public class CommentsAdapterViewHolder {
        @ViewInject(R.id.user_header_iv)
        public CircleImageView user_header_iv;
        @ViewInject(R.id.userName_tv)
        public TextView userName_tv;
        @ViewInject(R.id.commit_time_tv)
        public TextView commit_time_tv;
        @ViewInject(R.id.comments_content_tv)
        public TextView comments_content_tv;
    }
}
